package interfaz;

import imagenes.JPanelImage;

import java.awt.Choice;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import maquinas.Tractor;
import TDAListaSE.EmptyListException;
import TDAListaSE.Iterator;
import TDAListaSE.ListaSE;
import TDAListaSE.NoSuchElementException;
import control.Sistema;


//clase para modelar el panel de maquinas de visualizacion
public class V_Maquinas implements ItemListener {

	protected Choice o_marca;
	protected Sistema SIS;
	protected JPanel panelM;
	protected JPanel panelF;
	protected JPanelImage panelParaFoto;
	protected ListaSE<String> mostrando = null; //lista de fotos para mostrar
	protected JLabel labelImagen;
	
	protected JLabel l1 = new JLabel("Seleccione una Maquina: ");
	
	//lista para almacenar los choices
	protected ListaSE<Choice> choices;
	
	public V_Maquinas(JPanel pMaquinas, JPanel pFoto, Sistema s){
		choices = new ListaSE<Choice>();
		SIS = s;
		panelM = pMaquinas;
		panelF = pFoto;
		cargarEtiquetas();
		panelParaFoto = new JPanelImage(panelF.getWidth(), panelF.getHeight());
		panelF.add(panelParaFoto);
		cargarChoices();
	}
	
	private void cargarEtiquetas(){
		l1.setBounds(13, 30, 150, 20);
		panelM.add(l1);
	}
	
	private void cargarChoices(){
		o_marca = new Choice();
		o_marca.setBounds(10, 50, 200, 25);
		panelM.add(o_marca);
		o_marca.addItemListener(this);
		Iterator<Tractor> it = SIS.getTractores().iterator();
		while(it.hasNext()){
			try {
				Tractor aux = it.next(); 
				o_marca.add(aux.getMarca() + " - " + aux.getModelo() + " - " + aux.getAnio());
			} catch (NoSuchElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	private void resize(InputStream input, OutputStream output, int width, int height) throws Exception {
	    BufferedImage src = ImageIO.read(input);
	    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	    Graphics2D g = dest.createGraphics();
	    AffineTransform at = AffineTransform.getScaleInstance((double)width / src.getWidth(), (double)height / src.getHeight());
	    g.drawRenderedImage(src, at);
	    ImageIO.write(dest, "JPG", output);
	    output.close();
	}

	
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		
		mostrando = SIS.getFotos(o_marca.getSelectedIndex());
		Iterator<String> it = mostrando.iterator();
		while(it.hasNext()){
			try {
				System.out.println(it.next());
			} catch (NoSuchElementException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		try {
			
			panelParaFoto.setFoto(mostrando.first().element());
			
			
		} catch (EmptyListException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
	}

}
